//
//  CPSpinnerButtonView.m
//  CPSpinnerButtonView
//
//  Created by MorpLCP on 16/5/10.
//  Copyright © 2016年 MorpLCP. All rights reserved.
//

#import "CPSpinnerButtonView.h"
#import <Masonry.h>

#define kTopHeight 40
#define kBaseTag 10000

#define kSelfWidth self.frame.size.width
#define kSelfHeight self.frame.size.height

@interface CPSpinnerButtonView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, assign) NSInteger itemsCount;
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation CPSpinnerButtonView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, kTopHeight)];
        self.lineView = [UIView new];
        self.bacScrollView = [[UIScrollView alloc] init];
        self.bacScrollView.delegate = self;
        self.bacScrollView.pagingEnabled = YES;
        [self addSubview:_topView];
        [self addSubview:self.bacScrollView];
        [self initDefaultValue];
    }
    return self;
}

// 初始化默认值
- (void)initDefaultValue{
    self.selectIndex = 0;
    self.minGap = 5.0f;
    self.lineHeight = 5.0f;
    self.tintColor = [UIColor whiteColor];
    self.selectColor = [UIColor blueColor];
    self.unSelectColor = [UIColor grayColor];
}

// 添加内容
- (void)addContentItems:(NSMutableArray<UIView *> *)items Titles:(NSArray<NSString *> *)titles{
    if (items.count != titles.count) {
        NSLog(@"傻吊");
        return;
    }
    self.itemsCount = items.count;
    self.bacScrollView.contentSize = CGSizeMake(kSelfWidth * items.count, self.bacScrollView.frame.size.height);
    self.bacScrollView.frame = CGRectMake(0, _topView.frame.size.height + _lineHeight, kSelfWidth, CGRectGetHeight([[UIScreen mainScreen] bounds]) - _topView.frame.size.height - _lineHeight);
    
    
    self.lineView.frame = CGRectMake(_minGap, self.topView.frame.size.height, (kSelfWidth - _minGap * 2) / (items.count * 1.0), self.lineHeight);
    self.lineView.backgroundColor = self.selectColor;
    self.topView.backgroundColor = self.tintColor;
    [self addSubview:self.lineView];
    for (int i = 0; i < items.count; i++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn.tag = kBaseTag + i;
        if (self.selectIndex == i) {
            [btn setTitleColor:self.selectColor forState:(UIControlStateNormal)];
        } else{
            [btn setTitleColor:self.unSelectColor forState:(UIControlStateNormal)];
        }
        [btn setTitle:titles[i] forState:(UIControlStateNormal)];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        btn.frame = CGRectMake(_minGap + (kSelfWidth - _minGap * 2) / (items.count * 1.0) * i, 0, (kSelfWidth - _minGap * 2) / (items.count * 1.0), self.topView.frame.size.height);
        [self.topView addSubview:btn];
        items[i].frame = CGRectMake(kSelfWidth * i, 0, kSelfWidth, self.bacScrollView.frame.size.height);
        [self.bacScrollView addSubview:items[i]];
    }
}

- (void)btnClick:(UIButton *)sender{
    if (_selectIndex == sender.tag - 10000) {
        return;
    }
    self.selectIndex = sender.tag - 10000;
    [self.bacScrollView setContentOffset:CGPointMake(kSelfWidth * (sender.tag - kBaseTag), 0) animated:YES];
    if ([self.delegate respondsToSelector:@selector(buttonView:didSelectItem:)]) {
        [self.delegate performSelector:@selector(buttonView:didSelectItem:) withObject:self withObject:@(sender.tag - 10000)];
    }
}

- (void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex = selectIndex;
    for (UIView *v in _topView.subviews) {
        if ([v isKindOfClass:[UIButton class]]) {
            if ((v.tag - 10000) == selectIndex) {
                UIButton *btn = (UIButton *)v;
                [btn setTitleColor:_selectColor forState:(UIControlStateNormal)];
            } else{
                UIButton *btn = (UIButton *)v;
                [btn setTitleColor:_unSelectColor forState:(UIControlStateNormal)];
            }
        }
    }
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isKindOfClass:[UIScrollView class]]) {
        CGRect newFrame = self.lineView.frame;
        newFrame.origin.x = _minGap + newFrame.size.width *((scrollView.contentOffset.x) / kSelfWidth);
        self.lineView.frame = newFrame;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.selectIndex = (scrollView.contentOffset.x) / kSelfWidth;
}

@end
